package com.home.cop.sysinfo.service.impl;

import com.home.cop.enums.sysinfo.SysDataDictLevelEnum;
import com.home.cop.enums.sysinfo.SysDataDictStatuslEnum;
import com.home.cop.sysinfo.mapper.SysDataDictMapper;
import com.home.cop.sysinfo.model.SysDataDict;
import com.home.cop.sysinfo.service.SysDataDictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class SysDataDictServiceImpl implements SysDataDictService {

    @Autowired
    private SysDataDictMapper dataDictMapper;
    @Override
    public List<SysDataDict> list(String code) {
        if(StringUtils.isNotBlank(code)){
            DecimalFormat df = new DecimalFormat("000");
            String startCode=code.substring(0,4)+"0000";
            String endCode =code.substring(0,1)+df.format(Integer.valueOf(code.substring(1,4))+1)+"0000";
            return dataDictMapper.list(startCode,endCode);
        }
        return dataDictMapper.list(null,null);
    }

    @Override
    public List<SysDataDict> listTree() {
        List<SysDataDict> list = dataDictMapper.listAll();
        List<SysDataDict> parList = new ArrayList<>();
        List<SysDataDict> childList = new ArrayList<>();
        for (SysDataDict dataDict:list){
            if (dataDict.getSysDictLevel()==SysDataDictLevelEnum.PARENT.getCode()){
                parList.add(dataDict);
                dataDict.setChildren(new ArrayList<SysDataDict>());
            }else {
                childList.add(dataDict);
            }
        }
        Collections.sort(childList);
        for (SysDataDict chirdData:childList){
            for (SysDataDict parData:parList){
                if (chirdData.getSysDictCode().substring(0,4).equals(parData.getSysDictCode().substring(0,4))){
                    if (parData.getSysDictStatus()==SysDataDictStatuslEnum.DISABLE.getCode()){
                        chirdData.setSysDictStatus(SysDataDictStatuslEnum.DISABLE.getCode());
                    }
                    parData.getChildren().add(chirdData);
                    break;
                }
            }
        }
        Collections.sort(parList);
        return parList;
    }

    @Transactional
    @Override
    public void insert(SysDataDict dict) {
        dataDictMapper.insertSelective(dict);
    }

    @Transactional
    @Override
    public void delete(String sysDictCode) {
        dataDictMapper.deleteByPrimaryKey(sysDictCode);
    }

    @Transactional
    @Override
    public void changeStatus(String sysDictCode, Integer status) {
        dataDictMapper.changeStatus(sysDictCode,status);
    }

    @Override
    public SysDataDict info(String code) {
        return dataDictMapper.selectByPrimaryKey(code);
    }

    public static void main(String[] args) {
        DecimalFormat df = new DecimalFormat("000");
        String code = "C0170001";
        System.out.println(code.substring(0,4)+"0000");
        System.out.println(Integer.valueOf(code.substring(1,4))+1);
        System.out.println(code.substring(0,1)+df.format(Integer.valueOf(code.substring(1,4))+1)+"0000");
    }
}
